﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using QQ.Util;
using System.Threading;
using RuoTeng.Util;
using System.Windows.Forms;
using System.Net;
using AmazonProductAdvtApi;
using System.Xml;
using RTCRMQQ1.lib;

namespace RTGather.Lib.gather
{

    /// <summary>
    ///亞馬遜網站採集
    ///
    /// </summary>
    public class fbb_bugamazon
    {

        private string asinpattern = @"(?isx)ASIN%3D(?<asin>.*)";//asin        

        private string titlePattern = @"(?isx)<title>Amazon.com: (?<title>.*?)</title>";//标题  

        private string descPattern = @"(?isx)<h2>Product\sDescription</h2>(?<desc>.*?)<div\sid=\u0022purchase-sims-feature\u0022>";//描述

        public SignedRequestHelper helper = new SignedRequestHelper(MY_AWS_ACCESS_KEY_ID, MY_AWS_SECRET_KEY, DESTINATION);

        private static string rootpath = Application.StartupPath;//根目录 

        private string oldgather = rootpath + "\\已采集产品.txt";

        //=================亚马逊API调用参数==============================
        private const string MY_AWS_ACCESS_KEY_ID = "AKIAJ5NHDDVN7U2Q4ALQ";
        private const string MY_AWS_SECRET_KEY = "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";
        private const string DESTINATION = "ecs.amazonaws.com";

        private const string NAMESPACE = "http://webservices.amazon.com/AWSECommerceService/2009-03-31";
       
        //=====================================================================================================
         
        private List<Product> clist = new List<Product>();

        public void getData(string url,string code,string format)
        {

            WebRequest request = HttpWebRequest.Create(url);
            WebResponse response = request.GetResponse();
            XmlDocument doc = new XmlDocument();
            doc.Load(response.GetResponseStream());
            XmlNodeList plistUrl = doc.GetElementsByTagName("DetailPageURL", NAMESPACE);




            foreach (XmlNode temnode in plistUrl)
            {
                string innerurl = temnode.InnerText;

                string asin = getStrFromPattern(asinpattern, innerurl, "asin");
                if (IsRepeat(asin, oldgather) == false)
                {
                    //===========================进入内页===============================
                    string innerhtml = Http.GetData(innerurl, null);

                    string title = getStrFromPattern(titlePattern, innerhtml, "title");

                    string desc = getStrFromPattern(descPattern, innerhtml, "desc");
                    //描述进一步处理

                   desc =desc.Replace("\n","");

                   if (desc == "没有登记")
                   {
                       desc = title;
                   }

                    Product p = new Product();
                    p.desc = desc;
                    p.Name = title;
                    p.Pid = asin;
                    p.vcode = makeCode(code, asin);
                    writeNew(asin, oldgather);
                    clist.Add(p);
                }
            }
 
        }


        /// <summary>
        /// 判断是否重复
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private bool IsRepeat(string url, string filename)
        {
            List<string> list = TextBoxTools.getFileList(filename);
            bool isrepeat = list.Contains(url);
            return isrepeat;
        } 

        /// <summary>
        /// 写入新的记录
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="filename"></param>
        private void writeNew(string pid, string filename)
        {
            string old = RTFile.ReadFile(filename);
            string newstring = old + pid + System.Environment.NewLine;
            RTFile.WriteFile(filename, newstring); 
        }
         
        public string getStrFromPattern(string pattern,string content,string name)
        {
            Match match = Regex.Match(content, pattern);
            string result="";
            if (match.Success)
            {
                GroupCollection groups = match.Groups;
                result = groups[name].ToString();
            }
            else
            {
                result = "没有登记";
            }
            return result; 
        }

        public string makeCode(string code, string pid)
        {
            string result = string.Format("<a href=\"http://www.amazon.com/gp/product/{1}/ref=as_li_qf_br_asin_il_tl?ie=UTF8&tag={0}&linkCode=as2&camp=1789&creative=9325&creativeASIN={1}\">Learn more</a>", code, pid);
            return result;
        }

        /// <summary>
        /// 根据关键词搜索提取条数
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public int getCount(string keyword)
        {
            string url = getRequestUrl(keyword,1);
           
            XmlDocument doc = getDocment(url); 
            XmlNode titleNode = doc.GetElementsByTagName("TotalResults", NAMESPACE).Item(0);  
            int num = ParseType.ParseInt(titleNode.InnerText,0); 
            return num; 
        }


         

       

        /// <summary>
        /// 计算页数 
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public int getPageCount(int count)
        { 
            int PageSize = 10; 
            int pagenum = (count / PageSize) + 1; 
            return pagenum;
        }

        /// <summary>
        /// 生成请求的url
        /// </summary>
        /// <param name="keyword"></param>
        public string getRequestUrl(string keyword,int page)
        {
            String requestString = "Service=AWSECommerceService"
                   + "&Version=2009-03-31"
                   + "&Operation=ItemSearch"
                   + "&SearchIndex=All"
                   + "&ResponseGroup=Small"
                   + "&Keywords=" + keyword
                   + "&ItemPage=" + page
                   ;
            string requestUrl = helper.Sign(requestString);
            return requestUrl;
        }


        private XmlDocument getDocment(string url)
        {
             try
            {
                WebRequest request = HttpWebRequest.Create(url);
                WebResponse response = request.GetResponse();
                XmlDocument doc = new XmlDocument();
                doc.Load(response.GetResponseStream()); 
                XmlNodeList errorMessageNodes = doc.GetElementsByTagName("Message", NAMESPACE);
                if (errorMessageNodes != null && errorMessageNodes.Count > 0)
                {
                    String message = errorMessageNodes.Item(0).InnerText;
                     Console.WriteLine( "Error: " + message + " (but signature worked)");
                } 
                //XmlNode titleNode = doc.GetElementsByTagName("Title", NAMESPACE).Item(0); 
                return doc;
                
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Caught Exception: " + e.Message);
                System.Console.WriteLine("Stack Trace: " + e.StackTrace);
            }

            return null;

        }



         


        public string NoHTML(string Htmlstring)
        {
            //删除脚本
            Htmlstring = Htmlstring.Replace("\r\n", "");
            Htmlstring = Regex.Replace(Htmlstring, @"<script.*?</script>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<style.*?</style>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<.*?>", "", RegexOptions.IgnoreCase);
            //删除HTML
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            Htmlstring = Htmlstring.Replace("<", "");
            Htmlstring = Htmlstring.Replace(">", "");
            Htmlstring = Htmlstring.Replace("\r\n", "");
            return Htmlstring;
        }




        /// <summary>
        /// 导出 文件excel
        /// </summary>
        /// <param name="filename"></param>
        public void ExcelExport(string filename)
        {
            Excel.Application xls_app = null;
            Excel._Workbook xls_book = null;
            Excel._Worksheet xls_sheet = null;
            System.Reflection.Missing miss = System.Reflection.Missing.Value;
            try
            {
                xls_app = new Excel.ApplicationClass();
                xls_app.Visible = false;
                Excel.Workbooks workBooks = (Excel.Workbooks)xls_app.Workbooks;
                if (!System.IO.File.Exists(filename))
                {
                    xls_book = (Excel.Workbook)workBooks.Add(true);
                    Excel.Sheets _sheets = xls_book.Sheets;
                    xls_sheet = (Excel.Worksheet)_sheets[1];
                    int columnIndex = 1;

                    xls_sheet.Cells[1, columnIndex] = "产品编号"; columnIndex++; 
                    xls_sheet.Cells[1, columnIndex] = "产品标题"; columnIndex++; 
                   xls_sheet.Cells[1, columnIndex] = "产品描述"; columnIndex++;  
                    xls_sheet.Cells[1, columnIndex] = "产品代码"; columnIndex++; 
                   
                   
                    xls_book.SaveAs(filename, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss);
                    xls_book.Close(false, miss, miss);

                }
                xls_book = workBooks.Open(filename, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss);
                Excel.Sheets sheets = xls_book.Sheets;
                xls_sheet = (Excel.Worksheet)sheets[1];

                int rowIndex = xls_sheet.UsedRange.Rows.Count == 0 ? 1 : xls_sheet.UsedRange.Rows.Count + 1;

                foreach (Product c in this.clist)
                {

                    int columnIndex = 1;

                    xls_sheet.Cells[rowIndex, columnIndex] = c.Pid;
                    columnIndex++;


                    xls_sheet.Cells[rowIndex, columnIndex] = c.Name;
                    columnIndex++;

                    xls_sheet.Cells[rowIndex, columnIndex] = c.desc;
                    columnIndex++;

                    xls_sheet.Cells[rowIndex, columnIndex] = c.vcode;
                    columnIndex++;

                    rowIndex++;

                }
                xls_book.Save();
                xls_book.Close(false, miss, miss);
                xls_app.Quit();
            }
            catch(Exception ex) {
            	ex.ToString();            	if (xls_book != null) { xls_book.Save(); xls_book.Close(false, miss, miss); xls_app.Quit(); } }
        }


    }
}
